package great.project.config;

import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 * <div  style="border:2px solid none;margin:20px;padding:15px;font-size:24px;text-align:center;color:aqua;">
 *
 *
 *
 *  </div>
 *
 * @author zhangshun
 * @date 2021/2/5 16:26
 */
@Configuration
public class ValidationConfig {

	@Bean
	public Validator validator(final AutowireCapableBeanFactory autowireCapableBeanFactory) {
		ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
			.configure()
			.constraintValidatorFactory(new SpringConstraintValidatorFactory(autowireCapableBeanFactory))
			//设置有一个错误就直接返回
			.failFast(true)
			.buildValidatorFactory();
		return validatorFactory.getValidator();
	}
}
